import { ui } from "../ui/layaMaxUI";
import UIMoveScript from "./UIMoveScript";
import RoadPointsScript from "./RoadPointScript";
import EnemySpawner from "./EnemySpawner";
import Enemy from "./Enemy";
import Map from "./Map";
import WeaponData from "./WeaponData";

export default class StageScene extends ui.test.StageSceneUI {
  public static instance: StageScene
  public stageScene: Laya.Scene3D
  private icons = [this.icon0, this.icon1, this.icon2]
  private coinNum = 1000
  public weaponData: WeaponData = new WeaponData()
  public camera: Laya.Camera
  constructor() {
    super()
    StageScene.instance = this
    this.icons.forEach((icon, index) => {
      icon.icon.skin = 'icons/icon' + index + '.png'
      icon.price.text = (index + 1) * 100 + ''
      icon.on(Laya.Event.MOUSE_DOWN, this, this.onChse)
    })

    Laya.loader.create( [
      'res/atlas/Models/Conventional/BulletBoom.lh',
      'res/atlas/Models/Conventional/Enemy0Dead.lh',
      'res/atlas/Models/Conventional/Enemy1Dead.lh',
      'res/atlas/Models/Conventional/Enemy2Dead.lh',
      'res/atlas/Models/Conventional/Enemy3Dead.lh',
      'res/atlas/Models/Conventional/Bullet.lh',
      'res/atlas/Models/Conventional/Missile.lh'
    ],  Laya.Handler.create(this, this.onLoaded))
  }

  private onLoaded() {
    Laya.Scene3D.load('res/atlas/LayaScene_SampleScene/Conventional/SampleScene.ls', Laya.Handler.create(this, (scene: Laya.Scene3D) => {
      this.addChild(scene)
      this.stageScene = scene
      this.setChildIndex(scene, 0)

      // 获取摄像机
      var camera = scene.getChildByName('Main Camera') as Laya.Camera
      // 设置背景颜色
      camera.clearFlag = Laya.BaseCamera.CLEARFLAG_SOLIDCOLOR
      camera.clearColor = new Laya.Vector4(0.5, 0.5, 0, 1)
      this.camera = camera


      let uimove: UIMoveScript = camera.addComponent(UIMoveScript)
      this.on(Laya.Event.MOUSE_DOWN, this, (e: Laya.Event) => {
        uimove.rayHit()
      })

      // 获取路标集合
      let roadPoints = scene.getChildByName('RoadPoints') as Laya.Sprite3D
      roadPoints.addComponent(RoadPointsScript)

      // 获取敌人出现位置 
      let enemySpawn = scene.getChildByName('EnemySpawn') as Laya.Sprite3D
      enemySpawn.addComponent(EnemySpawner)

      // 获取地板
      let maps = scene.getChildByName('Map') as Laya.Sprite3D
      let len = maps.numChildren
      for (let i = 0; i < len; i++) {
        let map = maps.getChildAt(i) as Laya.Sprite3D
        let collder = map.getComponent(Laya.PhysicsCollider) as Laya.PhysicsCollider
        // 设置碰撞分组
        collder.collisionGroup = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER9
        map.addComponent(Map)
      }

      // // 加载模型
      // Laya.Sprite3D.load('res/atlas/Models/Conventional/Turret_up.lh', Laya.Handler.create(this, (turret: Laya.Sprite3D) => {
      //   scene.addChild(turret)
      //   turret.transform.position = new Laya.Vector3(0, 0, 0)
      // }))
      //   // 加载模型
      //   Laya.MeshSprite3D.load('res/atlas/Models/Conventional/Enemy2.lh', Laya.Handler.create(this, (enemy: Laya.MeshSprite3D) => {
      //     scene.addChild(enemy)
      //     enemy.transform.position = new Laya.Vector3(0, 10, 0)
      //   }))
      //   Laya.ShuriKenParticle3D.load('res/atlas/Models/Conventional/Enemy3Dead.lh', Laya.Handler.create(this, (enemy: Laya.ShuriKenParticle3D) => {
      //     scene.addChild(enemy)
      //     enemy.transform.position = new Laya.Vector3(0, 5, 0)
      //     enemy.particleSystem.isPlaying
      //   }))
    }))
  }

  public setCoin(value) {
    if (this.coinNum < value) return false
    this.coinNum -= value
    this.coins.text = this.coinNum + ''
    return true
  }

  private onChse(e: Laya.Event) {
    e.stopPropagation()
    // 选择武器
    let index = this.getChildIndex(e.target)
    // console.log('选择武器' + index)
    // 设置当前选择的武器
    this.weaponData.index = index - 1
  }

  /**
   *添加建造或者移除weapon事件
   * @param {Map} map
   * @memberof StageScene
   */
  public addBuildEvent(map: Map, level: number) {
    let CLICK = Laya.Event.CLICK
    this.add.offAll(CLICK)
    this.remove.offAll(CLICK)
    this.add.gray = level == 2

    this.buildAni.wrapMode = Laya.Animation.WRAP_REVERSE
    this.add.once(CLICK, this, () => {
      map.levelupTurrret()
      this.buildAni.play(0, false)
    })
    this.remove.once(CLICK, this, () => {
      map.removeTurret()
      this.buildAni.play(0, false)
    })
  }
}